package com.sky.controller.admin;

import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api("套餐接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealVO.categoryId")
    public Result save(@RequestBody SetmealVO setmealVO){

        setmealService.saveWithDish(setmealVO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public  Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        //@RequestBody 只接受请求体 一般接受json格式数据 如果前端get请求 无法进行参数映射赋值的
       PageResult pageResult= setmealService.pageQuery(setmealPageQueryDTO);
       return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("批量删除")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public  Result deleteByIds(@RequestParam List<Long> ids){

        setmealService.deleteByIds(ids);

        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询id")
    public  Result<SetmealVO> getById(@PathVariable Long id){
        SetmealVO setmealVO=setmealService.getById(id);
        return Result.success(setmealVO);

    }
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public  Result update(@RequestBody SetmealVO setmealVO){
        setmealService.update(setmealVO);
        return Result.success();
    }
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(Long id,@PathVariable Integer status){

        setmealService.startOrStop(id,status);
        return Result.success();
    }

}
